// pages/home/list.js
const net = require('../../../utils/net.js');
const util = require('../../../utils/util.js');
var posting = false;
var dialog = null;
const gd = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var sysinfo = gd.sysinfo;
    var w = sysinfo.windowWidth;
    console.log(sysinfo);
    this.setData({
      cardheight: (w - 100) * 29 / 46
    })
  },
  getlist() {
    if (posting) return;
    posting = true;
    var that = this;
    wx.showLoading({})
    net.request({
      url: '/CardList',
      data: {
        shop_id: gd.shop_id,
        member_id: gd.id
      }, success(res) {
        var dt = res.data;
        if (dt.flag) {
          if (dt.data.length > 0) {
            var tmplist = that.data.list;
            for (var i = 0; i < dt.data.length; ++i) {
              var time = dt.data[i]["start_date"].replace(/\-/g, ".") + "-" + dt.data[i]["end_date"].replace(/\-/g, ".");
              dt.data[i]["time"] = time;
              tmplist.push(dt.data[i]);
            }
            that.setData({
              page: that.data.page + 1,
              list: tmplist
            })
          } else {

          }
        } else {
          dialog.showDialog({
            msg: dt.msg
          })
        }
      }, fail(res) {

      }, complete() {
        posting = false;
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    })
  }, gocarddetail(e) {
    var id = e.currentTarget.dataset.id;
    util.Forward("../carddetail/carddetail?id=" + id);
  },  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    dialog = this.selectComponent("#dialog");
    this.getlist();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list: []
    })
    this.getlist();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})